package com.wr.modules.system.service.impl;

import cn.mybatis.mp.core.mvc.impl.BasicDaoImpl;
import com.wr.common.core.exception.ServiceException;
import com.wr.common.core.utils.BeanUtils;
import com.wr.common.core.utils.StringUtils;
import com.wr.common.core.utils.uuid.IdUtils;
import com.wr.api.system.domain.SysUserBo;
import com.wr.api.system.domain.SysUserWxBo;
import com.wr.common.security.utils.SecurityUtils;
import com.wr.modules.system.common.utils.RandomNameUtil;
import com.wr.modules.system.domain.sys.user.SysUser;
import com.wr.modules.system.domain.sys.user.expand.SysUserExpand;
import com.wr.modules.system.domain.sys.user.wx.SysUserWx;
import com.wr.modules.system.mapper.MybatisBasicMapper;
import com.wr.modules.system.mapper.SysUserMapper;
import com.wr.modules.system.service.ISysUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author MyPC
 */
@Service
public class SysUserServiceImpl extends BasicDaoImpl<SysUser, Long> implements ISysUserService {

    @Resource
    private MybatisBasicMapper mybatisBasicMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public SysUserBo findUserByUk(String uk) {
        return sysUserMapper.findUserByUk(uk);
    }

    @Override
    public SysUserBo findUserByMobile(String mobile, Boolean isCreate) {
        return sysUserMapper.findUserByMobile(mobile);
    }

    @Override
    public SysUserBo findUserByMailbox(String mailbox, Boolean isCreate) {
        return sysUserMapper.findUserByMailbox(mailbox);
    }

    @Override
    public SysUserBo findUserByWx(SysUserWxBo sysUserWxBo) {
        SysUserBo sysUserBo = sysUserMapper.findUserByWx(sysUserWxBo.getUnionId(), sysUserWxBo.getOpenId());
        if (StringUtils.isNotNull(sysUserBo)) {
            return sysUserBo;
        }
        registerWx(sysUserWxBo);
        return sysUserMapper.findUserByWx(sysUserWxBo.getUnionId(), sysUserWxBo.getOpenId());
    }


    @Override
    public Boolean registerUser(String uk, String pk) {
        SysUser sysUser = sysUserMapper.get(where -> {
            where.eq(SysUser::getUk, uk);
        });
        if (StringUtils.isNotNull(sysUser)) {
            throw new ServiceException("用户名已存在");
        }
        sysUser = new SysUser();
        sysUser.setUserId(IdUtils.getUuid());
        sysUser.setUk(uk);
        sysUser.setNickName(RandomNameUtil.getName());
        sysUser.setPk(SecurityUtils.encryptPassword(pk));
        return sysUserMapper.save(sysUser) > 0;
    }

    @Override
    public SysUserBo selectByIdBo(Long userId) {
        return sysUserMapper.selectByIdBo(userId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void registerWx(SysUserWxBo sysUserWxBo) {
        Long userId = IdUtils.getUuid();
        SysUserWx sysUserWx = BeanUtils.copyDataProp(sysUserWxBo, new SysUserWx());
        sysUserWx.setUserId(userId);
        mybatisBasicMapper.save(sysUserWx);
        SysUser sysUser = new SysUser();
        sysUser.setUserId(userId);
        sysUser.setNickName(sysUserWxBo.getNickName());
        sysUser.setAvatar(sysUserWxBo.getHeadImgUrl());
        sysUser.setSex(String.valueOf(sysUserWxBo.getSex()));
        sysUser.setRegisterType("3");
        sysUser.setCreateBy(String.valueOf(userId));
        sysUser.setCreateTime(new Date());
        if (sysUserMapper.save(sysUser) > 0) {
            SysUserExpand sysUserExpand = new SysUserExpand();
            sysUserExpand.setUserId(userId);
            sysUserExpand.setWechatLogin("Y");
            mybatisBasicMapper.save(sysUserExpand);
        } else {
            throw new ServiceException("微信注册失败！");
        }
    }
}
